﻿Option Explicit On
Option Strict On
Imports System.Data
Imports System.Text
Imports System.Configuration
Imports MySql.Data.MySqlClient

Public Class frmBrowserEqu

    Dim Conn As New MySqlConnection()
    Dim com As New MySqlCommand()
    Dim sb As New StringBuilder()
    Dim dr As MySqlDataReader
    Dim dt As DataTable
    Dim tr As MySqlTransaction

    Private Sub frmBrowserEqu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim strConn As String = ConfigurationManager.ConnectionStrings("Equ").ConnectionString
        If Conn.State = ConnectionState.Open Then
            Conn.Close()
        End If
        Conn.ConnectionString = strConn
        Conn.Open()
        ShowEqu()
        FormatEqu()
    End Sub

    Private Sub ShowEqu()
        sb.Remove(0, sb.Length)
        sb.Append("SELECT * FROM item")
        Dim sql As String = sb.ToString()
        With com
            .CommandText = sql
            .CommandType = CommandType.Text
            .Connection = Conn
            dr = .ExecuteReader()
            If dr.HasRows Then
                dt = New DataTable()
                dt.Load(dr)
                DataGrid.DataSource = dt
            Else
                DataGrid.DataSource = Nothing
            End If
            dr.Close()
        End With
    End Sub

    Private Sub FormatEqu()
        If DataGrid.RowCount > 0 Then
            With DataGrid
                .Columns(0).HeaderText = "รหัส"
                .Columns(1).HeaderText = "ชื่อ"
                .Columns(2).HeaderText = "ประเภท"
                .Columns(3).HeaderText = "จำนวน"
                .Columns(0).Width = 100
                .Columns(1).Width = 150
                .Columns(2).Width = 150
                .Columns(3).Width = 100
            End With
        End If
    End Sub

    Private Sub DataGrid_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid.CellDoubleClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        ID_equ = DataGrid.Rows(e.RowIndex).Cells("idItem").Value.ToString()
        Name_equ = DataGrid.Rows(e.RowIndex).Cells("Name").Value.ToString()
        Type_equ = DataGrid.Rows(e.RowIndex).Cells("Type").Value.ToString()
        DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Public ID_equ As String
    Public Name_equ As String
    Public Type_equ As String

    Public Property ID As String
        Get
            Return ID_equ
        End Get
        Set(value As String)
            ID_equ = value
        End Set
    End Property

    Public Property Name_e As String
        Get
            Return Name_equ
        End Get
        Set(value As String)
            Name_equ = value
        End Set
    End Property

    Public Property Type As String
        Get
            Return Type_equ
        End Get
        Set(value As String)
            Type_equ = value
        End Set
    End Property

    Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSearch.KeyDown
        If txtSearch.Text.Trim() = "" Then
            Exit Sub
        End If
        sb.Remove(0, sb.Length)
        sb.Append("SELECT * FROM item WHERE Name LIKE '%" & txtSearch.Text & "%'")
        Dim sql As String = sb.ToString()
        With com
            .CommandText = sql
            .CommandType = CommandType.Text
            .Connection = Conn
            dr = .ExecuteReader()
            If dr.HasRows Then
                dt = New DataTable()
                dt.Load(dr)
                DataGrid.DataSource = dt
            Else
                DataGrid.DataSource = Nothing
            End If
            dr.Close()
        End With
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        sb.Remove(0, sb.Length)
        sb.Append("SELECT * FROM item WHERE Name LIKE '%" & txtSearch.Text & "%'")
        Dim sql As String = sb.ToString()
        With com
            .CommandText = sql
            .CommandType = CommandType.Text
            .Connection = Conn
            dr = .ExecuteReader()
            If dr.HasRows Then
                dt = New DataTable()
                dt.Load(dr)
                DataGrid.DataSource = dt
            Else
                DataGrid.DataSource = Nothing
            End If
            dr.Close()
        End With
    End Sub
End Class